home *** CD-ROM | disk | FTP | other *** search
- Path: news.clark.net!usenet
- From: yom@clark.net (yom)
- Newsgroups: comp.lang.c
- Subject: Re: more problems with qsort
- Date: 28 Feb 1996 03:47:58 GMT
- Organization: Your Organization
- Message-ID: <4h0j9e$ng5@clarknet.clark.net>
- References: <177399702S86.JW1675A@american.edu>
- NNTP-Posting-Host: yom.clark.net
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- Since you're trying to sort a char**, the qsort function call must
- look like this:
-
- qsort(array,lines,sizeof(char **),(int (*)(void *,void *)) compare);
-
- And your compare function must be defined like this:
-
- int compare(char **a,char **b) {return strcmp(*a,*b);}
-
- The use of sizeof operator ensures that this code will be compatible
- with 64 bit architecture such as AXP/OSF.
-
- Song (yom@clark.net)
-
- In article <177399702S86.JW1675A@american.edu>, JW1675A@american.edu
- says...
- >
- >Hi folks --
- >
- >after reading the FAQ entries about qsort, I thought I had my problem
- >licked, but it seems not.
- >
- >Architecture: SunOS 4.1.3 with ANSI-C compiler.
- >
- >I'm doing some file manipulation and between steps A and B, I need to
- sort.
- >For various reasons, I don't want to popen() to the sort utility--I
- want to
- >use qsort. Here's what I'm doing:
- >
- > get number of lines in the file
- > (char**)malloc with enough room for all lines
- > for each line in the file {
- > (char*)malloc(90) /* 90 is enough room for each line in file */
- > copy each line into the newly malloc()ed space
- > point a (char**) to the newly malloc()ed space
- > }
- >
- >so now I should have "lines" number of pointers to pointers to char,
- >each one pointing to 90 bytes containing a line in the file.
- >
- >So, I call qsort(array[0], lines, 90, compare)
- >where compare is my comparison function -- prepared as discussed
- >in the FAQ. Now all I get are core dumps during the call to qsort().
- >:-)
- >
- >The core dump isn't in my compare routine -- it's somewhere else in
- >the qsort() function according to dbx -- so I'm sure it's a problem
- >setting up my data.
- >
- >Any tips? Much appreciation in advance,
- >Regards,
- >Jim
-
-